<?php
App::uses('AppController', 'Controller');
class CategoriesController extends AppController {
var $name = "Categories";// ten cua Controller
    var $helpers = array('Paginator','Html');
    var $paginate = array();
	var $components = array('Session', 'Link');
	public function beforeFilter() {
		//parent::beforeFilter();
		parent::getTopic();
		$this -> Auth -> allow(array('view'));
		
	}
	public function index() {
		$conditions = array("Category.deleted" => 0);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("Category",$conditions);
        $this->set("data",$data);
		$this->set("strTitle","Chuyên mục");
	}
	public function add(){
		$this->loadModel('Topic');
		
		$topics = $this->Topic->find('all');
		$this->set("topics", $topics);
		$this->set("strTitle","Thêm Chuyên mục");
		if ($this -> request -> is('post')) {
			$new = $this->data;
			$new['deleted'] = 0;
			$new['shortLink'] = $this->Link->getShortLink($new['name']);
			if ($this -> Category -> save($new)){
				$this->Session->setFlash('Thêm thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
		
	}
	public function edit($id){
		$this->set("strTitle","Cập nhật Chuyên mục");
		$this->loadModel('Topic');
		
		$topics = $this->Topic->find('all');
		$this->set("topics", $topics);
		
		$conditions = array("Category.id" => $id);
		$data = $this->Category->find('first', array('conditions' => $conditions));
		$this -> set("data", $data);
		if ($this -> request -> is('post')) {
			$new = $this->data;
			$new['deleted'] = 0;
			//$new['shortLink'] = $this->Link->getShortLink($new['name']);
			if ($this -> Category -> save($new)){
				$this->Session->setFlash('Thêm thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
		
	}
	public function delete($id){
		$conditions = array("Category.id" => $id);
		$data = $this-> Category ->find('first', array('conditions' => $conditions));
		$new = $data['Category'];
		$new['deleted'] = 1;
		if ($this -> Category -> save($new)){
			$this->Session->setFlash('Xóa thành công');
			$this->redirect(array('action' => 'index'));
		}
	}
	public function view($id){
		$this->set("strTitle","Danh sách bài viết");
		$this->loadModel('Post');
		$conditions = array("Post.catId" => $id);
		$data = $this-> Post ->find('all', array('conditions' => $conditions));
		$this -> set("data", $data);
	}
}